#ifndef PNG_IO_HPP
#define PNG_IO_HPP
/*
png_io.hpp
2014/11/13
psycommando@gmail.com
Description: Utilities for importing and exporting PNG images and their palettes into the formats used in the lib.
*/
#include "pmd2_image_formats.hpp"
#include "palettes.hpp"
//#include <png.hpp>
#include <algorithm>
#include "tiled_image.hpp"

namespace pmd2{ namespace pngio
{
    //template< int BIT_DEPTH > struct pngpp_pixel_t
    //{
    //    static_assert(false);
    //};

    //template<> struct pngpp_pixel_t<4>
    //{
    //    typedef png::packed_pixel<4> pt;
    //};

    //template<> struct pngpp_pixel_t<8>
    //{
    //    typedef png::index_pixel pt;
    //};

    //pmd2::graphics::colorRGB24 convertpngcolortorgb24( const png::color & acolor )
    //{
    //    return pmd2::graphics::colorRGB24( acolor.red, acolor.green, acolor.blue );
    //}


    //template< int IN_BIT_DEPTH, int OUT_BIT_DEPTH >
    //    void importPNG( std::vector<uint8_t> & out_tiledimage,
    //                    rgb24palette_t       & out_palette,
    //                    const std::string    & filepath )
    //{
    //    static_assert(OUT_BIT_DEPTH != 0 && OUT_BIT_DEPTH % 2 == 0 && OUT_BIT_DEPTH <= 8);
    //    static_assert( IN_BIT_DEPTH != 0 && IN_BIT_DEPTH % 2 == 0 && IN_BIT_DEPTH <= 8 );

    //    using pmd2::graphics::GetTiledIndexFromUntiledImageCoord;
    //    typedef pngpp_pixel_t<IN_BIT_DEPTH>::pt mypixeltype;

    //    //Open and read the file
    //    png::image<mypixeltype> inputimage;
    //    inputimage.read( filepath, png::require_color_space<mypixeltype>() );

    //    //Reserve output size
    //    out_tiledimage.resize( inputimage.get_height() * inputimage.get_width() * (OUT_BIT_DEPTH / IN_BIT_DEPTH) );
    //    out_palette.resize( inputimage.get_palette().size() );

    //    //Copy colors
    //    std::transform( inputimage.get_palette().begin(), 
    //                    inputimage.get_palette().end(), 
    //                    out_palette.begin(), 
    //                    out_palette.end(),
    //                    convertpngcolortorgb24 );

    //    //Check if input and output bitdepth match, and only then go straight for tiling
    //    if( IN_BIT_DEPTH == OUT_BIT_DEPTH )
    //    {
    //        //Convert byte per byte, and tile the image
    //        for( unsigned int i = 0; i < inputimage.get_width(); ++i )
    //        {
    //            for( unsigned int j = 0; j < inputimage.get_height(); ++j )
    //            {
    //                unsigned int index = GetTiledIndexFromUntiledImageCoord( i, j, inputimage.get_height() );
    //                out_tiledimage[index] =  static_cast<png::byte>( inputimage.get_pixel( i, j ) );
    //            }
    //        }
    //    }
    //    else
    //        assert(false); //Not implemented
    //}

    bool import4bppPNG_To_tiled_indexed_img( pmd2::graphics::tiled_indexed_image<pmd2::graphics::pixel_indexed_4bpp,pmd2::graphics::colorRGB24>  & out_indexed,
                                             const std::string                                   & filepath );


    bool export4bpp_tiled_indexed_imgToPNG_4bpp( const pmd2::graphics::tiled_indexed_image<pmd2::graphics::pixel_indexed_4bpp,pmd2::graphics::colorRGB24>  & in_indexed,
                                                 const std::string                                   & filepath );
};};

#endif